package org.gwt.pluginframework.desktop.client;

import org.gwt.pluginframework.common.Plugin;
import org.gwt.pluginframework.common.event.IPluginEvent;
import org.gwt.pluginframework.common.event.PluginEvent;
import org.gwt.pluginframework.common.event.PluginEventHandler;
import org.gwt.pluginframework.plugin.CustomEvents;

import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabPanel.TabPosition;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.Label;

public class FormB extends Plugin {

	private static final long serialVersionUID = -8663051182537577291L;

	protected TabPanel main;
	
	protected TabItem item;
	
	public void setupManifest() {
		manifest.setName("FormB");
		manifest.setVersion("1.0");
	}

	@Override
	public void onInitialize() {
		main = new TabPanel(); 
		main.setTabPosition(TabPosition.BOTTOM);
	
		item = new TabItem();
		item.setText("FormB");
		item.setLayout(new FitLayout());
		item.add(main);
	
		registerEventHandler(new PluginEventHandler(CustomEvents.START_FORM_B_EVENT) {
			public void handle(IPluginEvent event) {
				if(!isStarted()) {
					start();
					dispatch(new PluginEvent(CustomEvents.START_TAB_EVENT, 
							new String[] {"tabItem"}, new Object[] {item}));
				} else {
					dispatch(new PluginEvent(CustomEvents.FOCUS_TAB_EVENT,
							new String[] {"tabItem"}, new Object[] {item}));
				}
			}
		});
	}
	
	@Override
	public void onStart() {
		TabItem first = new TabItem("First");
		first.add(new Label("First tab!"));
		main.add(first);
		
		TabItem second = new TabItem("Second");
		second.add(new Label("Second tab!"));
		main.add(second);
	}
}
